﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fetco.entities;
using fetco.data;
using WebTime;

namespace fetco.service
{
    public class WebUserService
    {
        #region check data
        public static bool CheckLoginName(string login_name)
        {
            login_name = Utility.EscapeString(login_name);
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            long userid = webuserdao.GetWebUserId(login_name);
            if (userid != 0)
            {
                return true;
            }
            return false;
        }
        public static bool CheckPassword(long user_id,string oldpass)
        {
            oldpass = Utility.Encrypt(oldpass);
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            string verifypass = webuserdao.GetWebUserPass(user_id);
            if (oldpass == verifypass)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Get User
        public static WebUser GetWebUser(long user_id)
        {
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.GetWebUserBy(user_id);
        }
        public static WebUser GetWebUser(string login_name)
        {
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.GetWebUserBy(login_name);
        }

        public static ListWebUser GetListAvatar(string list_user_id)
        {
            list_user_id = Utility.EscapeString(list_user_id);
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.GetListWebUserById(list_user_id, WebUserField.Avatar.ToString());
        }
        #endregion

        #region Set User
        public static bool AddWebUser(WebUser webuser)
        {
            webuser.LoginName = Utility.EscapeString(webuser.LoginName);
            webuser.FirstName = Utility.EscapeString(webuser.FirstName);
            webuser.LastName = Utility.EscapeString(webuser.LastName);
            webuser.Password = Utility.Encrypt(webuser.Password);
            webuser.Avatar = "";
            webuser.Address = Utility.EscapeString(webuser.Address);
            webuser.CreateDate = WebTime.WebTime.DateToLong();
            webuser.Published = 1;
            webuser.Status = 1;

            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            webuserdao.SetWebUser(webuser);
            if (webuser.UserId != 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Remove User
        public static bool RemoveWebUser(long user_id)
        {
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.DelWebUser(user_id);
        }
        #endregion

        #region Update User
        public static bool UpdateWebUser(WebUser webuser)
        {
            webuser.FirstName = Utility.EscapeString(webuser.FirstName);
            webuser.LastName = Utility.EscapeString(webuser.LastName);
            webuser.Avatar = Utility.Encrypt(webuser.Avatar);
            webuser.Address = Utility.EscapeString(webuser.Address);
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.UpdateWebUser(webuser);
        }

        public static bool ChangeAvatar(long user_id,string path)
        {
            path = Utility.EscapeString(path);
            // field update
            string fieldname = WebUserField.Avatar.ToString();
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.UpdateWebUser(user_id, fieldname, path);
        }

        public static bool ChangePassword(long user_id, string newpass)
        {
            newpass = Utility.Encrypt(newpass);
            // field update
            string fieldname = WebUserField.Password.ToString();
            WebUserDAO webuserdao = WebUserDAO.GetInstance();
            return webuserdao.UpdateWebUser(user_id, fieldname, newpass);
        }
        #endregion
    }
}
